package tongcheng.com.tongcheng.ui.yemian.shenfen.activity.install;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yanzhenjie.nohttp.RequestMethod;

import butterknife.Bind;
import butterknife.ButterKnife;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.bean.StatusBean;
import tongcheng.com.tongcheng.bean.YueDetialBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.utils.Setting;

public class JieYueActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.title_bar)
    RelativeLayout mBg;
    @Bind(R.id.back_text)
    TextView mBackText;
    @Bind(R.id.title_line)
    View mTitleLine;
    @Bind(R.id.btn1)
    Button mBtn1;
    @Bind(R.id.btn2)
    Button mBtn2;
    @Bind(R.id.text1)
    TextView mText1;
    @Bind(R.id.text2)
    TextView mText2;
    private String mQianyue;
    private int mId;


    @Override
    public Integer initResid() {
        return R.layout.activity_jie_yue;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {
        settitlebar(mBg);



        mQianyue = getIntent().getStringExtra("qianyue");
        mId = getIntent().getIntExtra("id", -1);
        Log.e("解约id", mId + "");
        Log.e("解约id", Setting.getId() + "");
        Log.e("解约id", mQianyue + "");
        initYueState();
        switch (mQianyue) {
            case "2":
                mTitle.setText("解约用户协议");
                mBtn1.setText("确认解约");
                mBtn2.setText("确认解约");
                break;
            case "3":
                mTitle.setText("签约用户协议");
                mBtn1.setText("确认签约");
                mBtn2.setText("确认签约");
                break;
            case "-1":
                mTitle.setText("签约用户协议");
                mBtn1.setText("确认签约");
                mBtn2.setText("确认签约");
                break;
        }

        if (Setting.getShenfen() == 3) {//代理商
            mBtn1.setClickable(true);
            mBtn1.setBackgroundResource(R.drawable.bg_xian_small_yellow);
            mBtn2.setBackgroundResource(R.drawable.bg_xian_small);
            mBtn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String trim = mBtn1.getText().toString().trim();
                    if (trim.equals("确认签约")) {
                        DQianyue();
                    } else if (trim.equals("确认解约")) {
                        DJieyue();
                    }


                }


            });
            /*mBtn2.setClickable(false);*/
        } else {//安装人员
           /* mBtn1.setClickable(false);*/
            mBtn2.setBackgroundResource(R.drawable.bg_xian_small_yellow);
            mBtn1.setBackgroundResource(R.drawable.bg_xian_small);
            mBtn2.setClickable(true);
            mBtn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String trim = mBtn2.getText().toString().trim();
                    if (trim.equals("确认签约")) {
                        IQianyue();
                    } else if (trim.equals("确认解约")) {
                        IJieyue();
                    }
                }
            });
        }


        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initYueState() {
        MyRequest request = new MyRequest(ConStans.YueDetial_Url, RequestMethod.POST);
        request.add("usreid", Setting.getId());
        request.add("searchid", mId);
        CallServer.getInstance().addPost(this, request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
               /* MyRequest request = new MyRequest(ConStans.Jieyue_Url, RequestMethod.POST);*/
                YueDetialBean yueDetialBean = GsonUtil.GsonToBean(json, YueDetialBean.class);
                if (yueDetialBean != null) {
                    if (yueDetialBean.data != null) {
                        YueDetialBean.DataBean data = yueDetialBean.data;
                        if (yueDetialBean.status == 1) {
                            if (data.sign_type == 1) {//签约

                                if (data.install_status == 2) {

                                    mText2.setVisibility(View.VISIBLE);
                                    mText2.setText("安装人员以解约");
                                } else {

                                    mText2.setVisibility(View.INVISIBLE);
                                }


                                if (data.factor_status == 2) {

                                    mText1.setVisibility(View.VISIBLE);
                                    mText1.setText("代理商以解约");
                                } else {
                                    mText1.setVisibility(View.INVISIBLE);

                                }

                            } else if (data.sign_type == 2) {//解约

                                if (data.install_status == 2) {
                                    mText1.setVisibility(View.INVISIBLE);
                                    mText2.setVisibility(View.VISIBLE);
                                    mText2.setText("安装人员以签约");
                                } else {
                                    mText1.setVisibility(View.INVISIBLE);
                                    mText2.setVisibility(View.INVISIBLE);
                                }


                                if (data.factor_status == 2) {
                                    mText2.setVisibility(View.INVISIBLE);
                                    mText1.setVisibility(View.VISIBLE);
                                    mText1.setText("代理商以签约");
                                } else {
                                    mText1.setVisibility(View.INVISIBLE);
                                    mText2.setVisibility(View.INVISIBLE);
                                }
                            }
                        }
                    }
                }

            }
        });
    }

    //安装人员解约
    private void IJieyue() {
        MyRequest request = new MyRequest(ConStans.YueDetial_Url, RequestMethod.POST);
        request.add("usreid", Setting.getId());
        request.add("searchid", mId);
        CallServer.getInstance().addPost(this, request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
               /* MyRequest request = new MyRequest(ConStans.Jieyue_Url, RequestMethod.POST);*/
                YueDetialBean yueDetialBean = GsonUtil.GsonToBean(json, YueDetialBean.class);
                if (yueDetialBean != null) {
                    if (yueDetialBean.data != null) {
                        YueDetialBean.DataBean data = yueDetialBean.data;
                        if (yueDetialBean.status == 1) {
                            //安装人员解约
                            jieyue_I(data.id);

                        }

                    }
                }

            }
        });


    }

    private void jieyue_I(int id) {
        MyRequest request = new MyRequest(ConStans.Jieyue_Url, RequestMethod.POST);
        request.add("signid", id + "");
        request.add("identity", 5 + "");
        CallServer.getInstance().addPost(this, request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                StatusBean statusBean = GsonUtil.GsonToBean(json, StatusBean.class);
                if (statusBean != null) {
                    if (statusBean.status == 1) {
                        finish();
                    }
                }
            }
        });
    }

    //安装人员签约
    private void IQianyue() {
        MyRequest request = new MyRequest(ConStans.Qianyue_Url, RequestMethod.POST);
        request.add("factor_id", mId + "");
        request.add("install_id", Setting.getId() + "");
        request.add("identity", 5 + "");
        CallServer.getInstance().addPost(this, request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                StatusBean statusBean = GsonUtil.GsonToBean(json, StatusBean.class);
                if (statusBean != null) {
                    if (statusBean.status == 1) {
                        finish();
                    }
                }
            }
        });

    }

    //代理商解约
    private void DJieyue() {
        MyRequest request = new MyRequest(ConStans.YueDetial_Url, RequestMethod.POST);
        request.add("usreid", Setting.getId());
        request.add("searchid", mId);
        CallServer.getInstance().addPost(this, request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
               /* MyRequest request = new MyRequest(ConStans.Jieyue_Url, RequestMethod.POST);*/
                YueDetialBean yueDetialBean = GsonUtil.GsonToBean(json, YueDetialBean.class);
                if (yueDetialBean != null) {
                    if (yueDetialBean.data != null) {
                        YueDetialBean.DataBean data = yueDetialBean.data;
                        if (yueDetialBean.status == 1) {
                            //安装人员解约
                            jieyue_D(data.id);

                        }

                    }
                }

            }
        });

    }

    private void jieyue_D(int id) {
        MyRequest request = new MyRequest(ConStans.Jieyue_Url, RequestMethod.POST);
        request.add("signid", id + "");
        request.add("identity", 3 + "");
        CallServer.getInstance().addPost(this, request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                StatusBean statusBean = GsonUtil.GsonToBean(json, StatusBean.class);
                if (statusBean != null) {
                    if (statusBean.status == 1) {
                        finish();
                    }
                }
            }
        });
    }

    //代理商签约
    private void DQianyue() {
        MyRequest request = new MyRequest(ConStans.Qianyue_Url, RequestMethod.POST);
        request.add("factor_id", Setting.getId() + "");
        request.add("install_id", mId + "");
        request.add("identity", 3 + "");
        CallServer.getInstance().addPost(this, request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                StatusBean statusBean = GsonUtil.GsonToBean(json, StatusBean.class);
                if (statusBean != null) {
                    if (statusBean.status == 1) {
                        finish();
                    }
                }
            }
        });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
